<template>
  <div class="main-box">
    <div class="login-box">
      <div class="title">电力巡检系统</div>
      <el-form
        ref="ruleFormRef"
        style="width: 50%"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="auto"
        label-position="top"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="pass">
          <el-input
            v-model="ruleForm.pass"
            type="password"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="密码" prop="checkPass">
          <el-input
            v-model="ruleForm.checkPass"
            type="password"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item style="">
          <el-button type="primary" @click="submitForm(ruleFormRef)">
            登录
          </el-button>
          <el-button @click="resetForm(ruleFormRef)">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import type { FormInstance, FormRules } from "element-plus";

const ruleFormRef = ref<FormInstance>();

const router = useRouter();

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("直接点击登录"));
  } else {
    if (ruleForm.checkPass !== "") {
      if (!ruleFormRef.value) return;
      ruleFormRef.value.validateField("checkPass");
    }
    callback();
  }
};

const ruleForm = reactive({
  pass: "",
  checkPass: "",
  age: "",
});

const rules = reactive<FormRules<typeof ruleForm>>({
  pass: [{ validator: validatePass, trigger: "blur" }],
});

const submitForm = (formEl: FormInstance | undefined) => {
  //   if (!formEl) return;
  //   formEl.validate((valid) => {
  //     if (valid) {
  //       console.log("submit!");
  //     } else {
  //       console.log("error submit!");
  //     }
  //   });
  router.push({ name: "work_wait" });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>

<style scoped>
.title {
  font-size: 25px;
  font-weight: bold;
  margin-bottom: 1rem;
}
.main-box {
  width: 100%;
  height: 100%;
  background: #a4cbf5;
}
.login-box {
  position: absolute;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  top: calc(50% - 40% / 2);
  left: calc(50% - 40% / 2);
  height: 40%;
  width: 40%;
  background-color: #c6e2ff;
  border-radius: 10px;
}
</style>
